package cn.star.jots.base.file;

import cn.star.jots.base.response.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/v1/thumbnail")
@Api(tags = "电影视频上传")
@Slf4j
public class UploadFilmPreviewThumbnailController {
    @PostMapping("/upload")
    @ApiOperation(value = "上传图片文件")
    public JsonResult upload(MultipartFile file) throws IOException {
        log.info("上传文件:{}", file.getOriginalFilename());
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID() + suffix;
        String dirPath = "D:/jots/thumbnail";
        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
        String datePath = sdf.format(new Date());
        File dirFile = new File(dirPath + datePath);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        String filePath = dirPath + datePath + fileName;
        file.transferTo(new File(filePath));
        log.debug("视频保存路径:{}", "/thumbnail"+ datePath + fileName);
        return JsonResult.ok("/thumbnail"+ datePath + fileName);
    }

    @PostMapping("/remove")
    @ApiOperation(value = "删除文件")
    public JsonResult remove(String thumbnailUrl){
        log.info("删除视频:{}",thumbnailUrl);
        new File("D:/jots"+thumbnailUrl).delete();
        return JsonResult.ok();
    }

}

